<?php
namespace app\controller;

use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Fileload extends BaseController
{
    public function down($id){
        try {
            $u = $this->M('files')->field('path,name')->where(['id' => $id])->find();
            if(!empty($u)){
                $this->M('files')->where(['id' => $id])->setInc('down_times');
                try {
                    $m = $this->M('mod_file')->field('mod_id')->where('file_id','=',$id)->find();
                    $this->M('mod')->where('id','=',$m['mod_id'])->setInc('downloads');
                    return response()->header(['Location'=>'https://down.schub.top/down/'.$id]);
                }catch (DataNotFoundException | ModelNotFoundException | DbException $e){
                    return pr(302);
                }
            }
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return pr(302);
        }
        return pr(302);
    }
}
